//
//  XDLMInvestViewController.m
//  xindailianmeng
//
//  Created by 许佳奇 on 17/1/17.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//********投资主页

#import "XDLMInvestViewController.h"
#import "XDLMInvestTableViewCell.h"
#import "XDLMInvestTwoTableViewCell.h"
#import "XDLMInvestThreeTableViewCell.h"
#import "ScrollView.h"

#import "AFHTTPSessionManager+POST.h"
#import "XDLMInvestModel.h"
#import "XDLMSanPostInfoViewController.h"
#import "XDLMSanPostListViewController.h"
#import "XDLMMiiddleWebViewController.h"

#define TCOLOR [UIColor colorWithRed:187.0/255.0 green:147.0/255.0 blue:115.0/255.0 alpha:1]
@interface XDLMInvestViewController ()<UITableViewDataSource,UITableViewDelegate,scrollImageDidDelegate>
@property (nonatomic, strong) ScrollView *scView;
@property (nonatomic, strong) NSMutableArray *dataSoure;
@property (nonatomic, strong) XDLMInvestModel *model;

@end

@implementation XDLMInvestViewController
{
    UITableView *_table;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self addViews];
    [self loadData];
    // Do any additional setup after loading the view.
}
- (void)loadData
{
    NSDictionary *param = @{@"method":@"investmentPageHome"};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject)
    {
//        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            NSMutableArray *mutableArray = responseObject[@"result"][@"indexPosts"];
            for (NSDictionary *dic in mutableArray) {
                self.model = [[XDLMInvestModel alloc] initWithDictionary:dic error:nil];
                [self.dataSoure addObject:self.model];
            }
            [_table reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error)
    {
//        NSLog(@"%@",error);
    }];
}
- (void)addViews
{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 50) style:UITableViewStyleGrouped];
    
    _table.dataSource = self;
    _table.delegate = self;
    _table.showsVerticalScrollIndicator = NO;
    [_table registerClass:[XDLMInvestTableViewCell class] forCellReuseIdentifier:@"XDLMInvestTableViewCell"];
    [_table registerClass:[XDLMInvestTwoTableViewCell class] forCellReuseIdentifier:@"XDLMInvestTwoTableViewCell"];
    [_table registerClass:[XDLMInvestThreeTableViewCell class] forCellReuseIdentifier:@"XDLMInvestThreeTableViewCell"];
    [self.view addSubview:_table];
    
}
#pragma mark -- TableDataSource && Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        XDLMInvestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XDLMInvestTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.section == 2 && indexPath.row == 0)
    {
        XDLMInvestThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XDLMInvestThreeTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else
    {
        XDLMInvestTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XDLMInvestTwoTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.dataSoure.count != 0) {
            
            cell.model = self.dataSoure[indexPath.row-1];

        }
        return cell;
    }
    
    
    return nil;
}
//row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    else if (section == 1)
    {
        return 1;
    }
    else return 4;
}
//row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 0.01;
    }
    else if (indexPath.section == 1 && indexPath.row == 0){
        return 129*kscale;
    }
    else if (indexPath.section == 2 && indexPath.row == 0){
        return 39*kscale;
    }
    return 90*kscale;
}
//headerView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return [self setScrollView];
    }
    else return nil;
}
//header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 175 * kscale;
    }
    else if (section == 1)
    {
        return 10;
    }
    else if (section == 2){
       return 8*kscale;
    }
    else
    return 0;
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        XDLMMiiddleWebViewController *web = [[XDLMMiiddleWebViewController alloc]init];
        web.splitStr = @"yxjhapp.html";
        [self.navigationController pushViewController:web animated:YES];
    }
    if (indexPath.row == 0 && indexPath.section == 2) {
        XDLMSanPostListViewController *listCtr = [[XDLMSanPostListViewController alloc] init];
        listCtr.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:listCtr animated:YES];
    }
    else if (indexPath.section == 2 && indexPath.row != 0) {
        XDLMSanPostInfoViewController *sanPost = [[XDLMSanPostInfoViewController alloc] init];
        sanPost.recordID = self.model.recordId;
        sanPost.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:sanPost animated:YES];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
//tableViewcell顶头
//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
//        [cell setSeparatorInset:UIEdgeInsetsZero];
//    }
//    
//    
//    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
//        [cell setLayoutMargins:UIEdgeInsetsZero];
//    }
//}
//
//
//- (void)viewDidLayoutSubviews {
//    if ([_table respondsToSelector:@selector(setSeparatorInset:)]) {
//        [_table setSeparatorInset:UIEdgeInsetsZero];
//    }
//    
//    
//    if ([_table respondsToSelector:@selector(setLayoutMargins:)]) {
//        [_table setLayoutMargins:UIEdgeInsetsZero];
//    }
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
////    return 12;//这个方法不写，或者return 0跟return 12的效果一样
//    return 0.01;//把高度设置很小，效果可以看成footer的高度等于0
//}

//设置banner图
- (UIView *)setScrollView {
        //广告轮播背景
        //    UIImageView * baImag = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0 * kscale, kScreenW , 108 * kscaleW)];
        //    baImag.image = [UIImage imageNamed:@"banner"];
        //    [_backView addSubview:baImag];
        ScrollView * scView = [[ScrollView alloc] initWithFrame:CGRectMake(0, 0 * kscale, kScreenW , 175 * kscale)];
        self.scView = scView;
        scView.currentPageTinColor = kColor_255_69_69;
        scView.isTireming= YES;
        scView.isPageControling = YES;
        scView.placeHolderImage = @"banner";
        scView.delegate = self;
        //    scView.currentPageTinColor = [UIColor darkGrayColor];
        //    scView.pageTinColor = [UIColor whiteColor];
        [scView.imageUrlArray addObjectsFromArray:@[@"banner"]];
        [scView reloadData];
    return scView;
}
#pragma mark --点击事件
//点击查看更多
- (void)InfoBtnClick
{
    
}
#pragma mark --scrollImageDidDelegate
- (void)scrollImageDidDelegate:(NSInteger)count
{
    
}

#pragma mark -- Getter
-(NSMutableArray *)dataSoure
{
    if (_dataSoure == nil) {
        _dataSoure = [[NSMutableArray alloc] init];
    }
    return _dataSoure;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden=NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
